package com.ruoyi.screen.service.impl;

import java.util.LinkedList;
import java.util.List;
import com.ruoyi.common.core.utils.DateUtils;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.screen.domain.DeviceFile;
import com.ruoyi.screen.domain.dto.MaterialsOfDevice;
import com.ruoyi.screen.domain.dto.Register;
import com.ruoyi.screen.domain.dto.RegisterResult;
import com.ruoyi.screen.mapper.DeviceFileMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.screen.mapper.DeviceMapper;
import com.ruoyi.screen.domain.Device;
import com.ruoyi.screen.service.IDeviceService;
import com.ruoyi.common.datascope.annotation.DataScope;
/**
 * 广告屏设备Service业务层处理
 * 
 * @author david
 * @date 2025-05-21
 */
@Service
public class DeviceServiceImpl implements IDeviceService 
{
    @Autowired
    private DeviceMapper deviceMapper;

    @Autowired
    private DeviceFileMapper deviceFileMapper;

    /**
     * 注册广告屏设备
     *
     * @param register 注册信息
     * @return 响应结果
     */
    public RegisterResult register(Register register){
        Device device = deviceMapper.selectDeviceBySerial(register.getSerial());
        if(device == null){
            device = new Device();
            device.setSerial(register.getSerial());
            device.setDeviceName(register.getT().toString());
            deviceMapper.insertDevice(device);
            RegisterResult registerResult = new RegisterResult();
            registerResult.setDeviceId(device.getDeviceId());
            registerResult.setDeviceName(device.getDeviceName());
            registerResult.setSerial(device.getSerial());
            registerResult.setFiles(null);
	        List<MaterialsOfDevice> materialsOfDevice=new LinkedList<>();
			registerResult.setFiles(materialsOfDevice);
            return registerResult;
        }
        DeviceFile deviceFile = new DeviceFile();
        deviceFile.setDeviceId(device.getDeviceId());
        List<MaterialsOfDevice> materialsOfDevice = deviceFileMapper.selectMaterialsOfDevice(deviceFile);
        RegisterResult registerResult = new RegisterResult();
        registerResult.setDeviceId(device.getDeviceId());
        registerResult.setDeviceName(device.getDeviceName());
        registerResult.setSerial(device.getSerial());
        registerResult.setFiles(materialsOfDevice);
        return registerResult;

    }

    @Override
    public Device selectDeviceBySerial(String serial) {
        return deviceMapper.selectDeviceBySerial(serial);
    }

    /**
     * 查询广告屏设备
     * 
     * @param deviceId 广告屏设备主键
     * @return 广告屏设备
     */
    @Override
    public Device selectDeviceByDeviceId(Long deviceId)
    {
        return deviceMapper.selectDeviceByDeviceId(deviceId);
    }
    /**
     * 所有广告屏设备列表
     *
     * @return 广告屏设备集合
     */
    public List<Device> allDeviceList(){
        return deviceMapper.allDeviceList();
    }
    /**
     * 查询广告屏设备列表
     * 
     * @param device 广告屏设备
     * @return 广告屏设备
     */
    @Override
	@DataScope(deptAlias = "adv_device", userAlias = "adv_device")
    public List<Device> selectDeviceList(Device device)
    {
        return deviceMapper.selectDeviceList(device);
    }

    /**
     * 新增广告屏设备
     * 
     * @param device 广告屏设备
     * @return 结果
     */
    @Override
    public int insertDevice(Device device)
    {
        device.setCreateTime(DateUtils.getNowDate());
        return deviceMapper.insertDevice(device);
    }

    /**
     * 修改广告屏设备
     * 
     * @param device 广告屏设备
     * @return 结果
     */
    @Override
    public int updateDevice(Device device)
    {
        device.setUpdateTime(DateUtils.getNowDate());
        return deviceMapper.updateDevice(device);
    }

    /**
     * 批量删除广告屏设备
     * 
     * @param deviceIds 需要删除的广告屏设备主键
     * @return 结果
     */
    @Override
    public int deleteDeviceByDeviceIds(Long[] deviceIds)
    {
        return deviceMapper.deleteDeviceByDeviceIds(deviceIds);
    }

    /**
     * 删除广告屏设备信息
     * 
     * @param deviceId 广告屏设备主键
     * @return 结果
     */
    @Override
    public int deleteDeviceByDeviceId(Long deviceId)
    {
        return deviceMapper.deleteDeviceByDeviceId(deviceId);
    }
}
